﻿using System;
using System.ComponentModel;
using System.Data;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.Task.Context
{
    public class User : Crud<Domain.User>, ITask.IUser
    {
        public event FormEvent.FormEventHandler OnGetUserId;

        protected void TriggerGetUserId(object source, FormEvent e)
        {
            if (OnGetUserId != null)
                OnGetUserId(source, e);
        }

        public void GetUserId(Domain.User data)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwGetUserIdDoWork;
                a.RunWorkerCompleted += BgwGetUserIdWorkerCompleted;
                a.RunWorkerAsync(data);
            }
        }

        public void GetUserById(Domain.User data)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwFetchDoWork;
                a.RunWorkerCompleted += BgFetchRunWorkerCompleted;
                a.RunWorkerAsync(data);
            }
        }

        #region Protected Backgroud Worker Function
        protected void BgwGetUserIdDoWork(object sender, DoWorkEventArgs e)
        {
            var data = (Domain.User)e.Argument;
            e.Result = new model.Table.User().GetUserId(data.Username, data.Userpass);
        }

        protected void BgwGetUserIdWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var a = (UInt64)e.Result;
            TriggerGetUserId(this,
                          new FormEvent(!a.Equals(0))
                          {
                              Value = a,
                              Exception = new Exception(a.Equals(0) ? "Invalid username or password." : "Fetch success.")
                          });
        }
        #endregion

        #region Protected Overridable Function
        protected override void BgwFetchDoWork(object sender, DoWorkEventArgs e)
        {
            var data = (Domain.User)e.Argument;
            e.Result = new model.Table.User().GetUserById(data.Id);
        }

        protected override void BgFetchRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var result = (DataTable)e.Result;
            Domain.User data = null;

            if (!result.Rows.Count.Equals(0))
                data = new Domain.User
                           {
                               Id = Convert.ToUInt64(result.Rows[0]["id"].ToString()),
                               FirstName = result.Rows[0]["first_name"].ToString(),
                               LastName = result.Rows[0]["last_name"].ToString(),
                               MidName = result.Rows[0]["middle_name"].ToString(),
                               Username = result.Rows[0]["user_name"].ToString(),
                               Userpass = result.Rows[0]["user_pass"].ToString(),
                               Role = result.Rows[0]["role"].ToString(),
                               Active = Convert.ToBoolean(result.Rows[0]["is_active"].ToString()),
                               DateCreated = Convert.ToDateTime(result.Rows[0]["date_created"].ToString())
                           };

            TriggerFetch(this,
                            new FormEvent(data != null) { Exception = data == null ? new Exception("No records found.") : null, Value = data });
        }

        protected override void BgwFetchListsDoWork(object sender, DoWorkEventArgs e)
        {
            //e.Result = new Model.Table.Employee().GetEmployeeAll();
        }

        protected override void BgFetchListsRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //var a = (DataTable)e.Result;
            //var data =
            //    (a.Rows.Cast<DataRow>().Select(
            //        dr =>
            //        new Domain.Employee { Id = (UInt32)dr["ID"], EmployeeName = dr["APPOINTMENT_STATUS_NAME"].ToString() })).
            //        ToList();
            //TriggerFetchLists(this, new FormEvent(!data.Count.Equals(0)) { Value = data });
        }

        protected override void BgwInsertDoWork(object sender, DoWorkEventArgs e)
        {

        }

        protected override void BgInsertRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        protected override void BgwUpdateDoWork(object sender, DoWorkEventArgs e)
        {

        }

        protected override void BgUpdateRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        protected override void BgwDeleteDoWork(object sender, DoWorkEventArgs e)
        {

        }

        protected override void BgDeleteRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        protected override void BgwToggleDoWork(object sender, DoWorkEventArgs e)
        {

        }

        protected override void BgToggleRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }
        #endregion
    }
}
